<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <button onclick="onclick">按钮</button>
    <script type="module">
      window.onclick = async () => {
        const response = await fetch(
          'http://localhost:2000/gis/chat/message/gen',
          {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({
              messagesList: [
                {
                  role: 'user',
                  content: [
                    {
                      type: 'text',
                      text: '你好',
                    },
                    {
                      type: 'text',
                      text: '1+1=？',
                    },
                  ],
                },
              ],
              modelConfig: {
                streaming: true,
              },
              chatSessionId: 'xx',
            }),
          },
        );

        const reader = response.body.getReader();
        let resPonseText = '';
        while (true) {
          const { done, value } = await reader.read();
          if (done) {
            break;
          }
          const text = new TextDecoder().decode(value);

          const resText = text.split('\n\n').filter(Boolean);
          resText.forEach((item) => {
            const chunk = JSON.parse(item.replace(/^data:\s+/, '').trim());
            if (chunk.type === 'chunk') {
              resPonseText += chunk.content;
            }
            if (chunk.type === 'end') {
              console.log(chunk.chatSessionId, 'end');
            }
          });
        }
      };
    </script>
  </body>
</html>
